import categories.CategoriesRegistry;
import categories.CustomerCategory;
import org.apache.log4j.Logger;
import org.supercsv.io.CsvListReader;
import org.supercsv.prefs.CsvPreference;
import readers.CSVRowReader;

import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

public class SovintelParser extends Parser {
    private final static Logger log = Logger.getLogger(SovintelParser.class.toString());

    public SovintelParser(CategoriesRegistry categoriesRegistry,
                          File inFile,
                          OutputWriters writers
    ) throws IOException {
        super(
                "Sovintel parser for " + inFile,
                new CSVRowReader(
                        new CsvListReader(
                                new FileReader(inFile),
                                CsvPreference.STANDARD_PREFERENCE
                        ),
                        true
                ),

                writers,
                categoriesRegistry);
    }

    private static final SimpleDateFormat DATE_TIME_FORMAT = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");


    protected CallRow convertToRow(List<?> values) throws ParseException {
        //DID, USERCODE, DEST, DATETIME, DURATION

        return new CallRow(
                Long.parseLong((String) values.get(1)), // cust id
                DATE_TIME_FORMAT.parse((String) values.get(3)), // datetime
                Long.parseLong((String) values.get(2)), // dest number
                Long.parseLong((String) values.get(4)) // duration min
        );
    }


    protected void postCorrectValues(CustomerCategory customerCategory, CallRow row) {
        super.postCorrectValues(customerCategory, row);

        if(customerCategory == CustomerCategory.White) {
            row.setDestinationNumber(Long.parseLong("10" + row.getDestNumber()));
        }        
    }
}
